using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;

namespace System.ServiceProcess
{
    /// <summary>
    /// Replace the Main() function
    /// </summary>
    public static class ProgHelper
    {
        /// <summary>
        /// Auto-build your installer program...
        /// </summary>
        public static void DoWork(ServiceBase[] ServicesToRun, System.String[] args)
        {
            bool cmdLineStart = false;

            //Checking weather command-line startup is required :
            foreach (System.String cmd in args)
            {
                if (cmd.ToLower() == "/c" || cmd.ToLower() == "/command")
                {
                    cmdLineStart = true;
                    break;
                }
            }

            if (cmdLineStart)
            {
                foreach (ServiceBase svc in ServicesToRun)
                {
                    if (svc is ICmdLineStarter)
                    {
                        ((ICmdLineStarter)svc).CmdStart(args);
                    }
                }

                System.Console.WriteLine("Click on keyboard to leave...");
                System.Console.ReadKey();
            }
            else
            {
                ServiceBase.Run(ServicesToRun);
            }

            if (cmdLineStart)
            {
                foreach (ServiceBase svc in ServicesToRun)
                {
                    if (svc is ICmdLineStarter)
                    {
                        ((ICmdLineStarter)svc).CmdStop();
                    }
                }
            }

        }
    }
}